package view.readerView;

import java.awt.EventQueue;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.ImageIcon;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import dao.UserDao;
import model.User;
import util.DbUtil;
import util.StringUtil;
import view.logOnView.LogOnFrm1;

import javax.swing.JButton;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.awt.Color;

public class UserImfSearchInterFrm extends JInternalFrame {
	private JTextField s_userNameTxt;
	private JTextField s_passWordTxt;
	private JTextField idTxt;
	private JTextField userNameTxt;
	private JTextField passWordTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField telTxt;
	private JTextField emailTxt;
	private JRadioButton manJrb;
	private JRadioButton femaleJrb ;
	private JComboBox userTypeJcb ;
	
	private LogOnFrm1 logOnFrm=new LogOnFrm1();
	private DbUtil dbUtil=new DbUtil();
	private UserDao userDao=new UserDao();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					UserImfSearchInterFrm frame = new UserImfSearchInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public UserImfSearchInterFrm() {
		getContentPane().setBackground(new Color(224, 255, 255));
		setIconifiable(true);
		setClosable(true);
		setTitle("个人信息查询");
		setBounds(100, 100, 1359, 861);
		
		JPanel userSearchPanel = new JPanel();
		userSearchPanel.setBackground(new Color(255, 255, 224));
		
		JPanel userPanel = new JPanel();
		userPanel.setBackground(new Color(255, 255, 224));
		
		JLabel title = new JLabel("个人信息查询");
		title.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/readerAdim/个人信息查询 (2).png")));
		title.setFont(new Font("楷体", Font.BOLD, 50));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(527)
					.addComponent(title)
					.addContainerGap(564, Short.MAX_VALUE))
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(userPanel, GroupLayout.DEFAULT_SIZE, 1319, Short.MAX_VALUE)
					.addContainerGap())
				.addGroup(groupLayout.createSequentialGroup()
					.addContainerGap()
					.addComponent(userSearchPanel, GroupLayout.DEFAULT_SIZE, 1320, Short.MAX_VALUE)
					.addGap(11))
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(Alignment.TRAILING, groupLayout.createSequentialGroup()
					.addGap(24)
					.addComponent(title)
					.addPreferredGap(ComponentPlacement.RELATED, 57, Short.MAX_VALUE)
					.addComponent(userSearchPanel, GroupLayout.PREFERRED_SIZE, 148, GroupLayout.PREFERRED_SIZE)
					.addGap(27)
					.addComponent(userPanel, GroupLayout.PREFERRED_SIZE, 498, GroupLayout.PREFERRED_SIZE)
					.addContainerGap())
		);
		
		JLabel id = new JLabel("编  号：");
		id.setFont(new Font("宋体", Font.BOLD, 25));
		id.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		idTxt = new JTextField();
		idTxt.setFont(new Font("宋体", Font.BOLD, 25));
		idTxt.setEditable(false);
		idTxt.setColumns(10);
		
		JLabel userName = new JLabel("用户名：");
		userName.setFont(new Font("宋体", Font.BOLD, 25));
		userName.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		userNameTxt = new JTextField();
		userNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		userNameTxt.setColumns(10);
		
		JLabel passWord = new JLabel("用户密码：");
		passWord.setFont(new Font("宋体", Font.BOLD, 25));
		passWord.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		passWordTxt = new JTextField();
		passWordTxt.setFont(new Font("宋体", Font.BOLD, 25));
		passWordTxt.setColumns(10);
		
		JLabel sex = new JLabel("性  别：");
		sex.setFont(new Font("宋体", Font.BOLD, 25));
		sex.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		manJrb = new JRadioButton("男");
		manJrb.setBackground(new Color(255, 255, 224));
		manJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		femaleJrb.setBackground(new Color(255, 255, 224));
		femaleJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(femaleJrb);
		
		JLabel userType = new JLabel("用户类型：");
		userType.setFont(new Font("宋体", Font.BOLD, 25));
		userType.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		JLabel tel = new JLabel("手机号码：");
		tel.setFont(new Font("宋体", Font.BOLD, 25));
		tel.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		telTxt = new JTextField();
		telTxt.setFont(new Font("宋体", Font.BOLD, 25));
		telTxt.setColumns(10);
		
		JLabel email = new JLabel("邮  件：");
		email.setFont(new Font("宋体", Font.BOLD, 25));
		email.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/book.png")));
		
		emailTxt = new JTextField();
		emailTxt.setFont(new Font("宋体", Font.BOLD, 25));
		emailTxt.setColumns(10);
		
		
		String []types= {"学生","老师","系统管理员","图书管理员"};
		
		userTypeJcb = new JComboBox(types);
		userTypeJcb.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton alterButton = new JButton("修 改");
		alterButton.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/readerAdim/修改.png")));
		alterButton.setFont(new Font("宋体", Font.BOLD, 28));
		alterButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userUpdateActionPerformed(e);
			}
		});
		
		JButton cancelButton = new JButton("取 消");
		cancelButton.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/bookAdmi/取消.png")));
		cancelButton.setFont(new Font("宋体", Font.BOLD, 28));
		cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				updateResetActionPerformed(e);
			}
		});
		GroupLayout gl_userPanel = new GroupLayout(userPanel);
		gl_userPanel.setHorizontalGroup(
			gl_userPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_userPanel.createSequentialGroup()
					.addGap(36)
					.addGroup(gl_userPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(email)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(emailTxt))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(id)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(sex)
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(manJrb)
							.addGap(30)
							.addComponent(femaleJrb)))
					.addGap(118)
					.addGroup(gl_userPanel.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(userName)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(userType)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(userTypeJcb, 0, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
					.addGap(113)
					.addGroup(gl_userPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(passWord)
							.addGap(18)
							.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 156, GroupLayout.PREFERRED_SIZE))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addComponent(tel)
							.addGap(18)
							.addComponent(telTxt, GroupLayout.PREFERRED_SIZE, 164, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap(224, Short.MAX_VALUE))
				.addGroup(gl_userPanel.createSequentialGroup()
					.addGap(225)
					.addComponent(alterButton)
					.addGap(213)
					.addComponent(cancelButton)
					.addContainerGap(748, Short.MAX_VALUE))
		);
		gl_userPanel.setVerticalGroup(
			gl_userPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_userPanel.createSequentialGroup()
					.addGroup(gl_userPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_userPanel.createSequentialGroup()
							.addGap(55)
							.addGroup(gl_userPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(id)
								.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addGap(58)
							.addGroup(gl_userPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(userName)
								.addComponent(userNameTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE)
								.addComponent(passWord)
								.addComponent(passWordTxt, GroupLayout.PREFERRED_SIZE, 37, GroupLayout.PREFERRED_SIZE))))
					.addGroup(gl_userPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_userPanel.createSequentialGroup()
							.addGap(49)
							.addGroup(gl_userPanel.createParallelGroup(Alignment.BASELINE)
								.addComponent(sex)
								.addComponent(manJrb)
								.addComponent(femaleJrb)
								.addComponent(userType)
								.addComponent(tel)
								.addComponent(userTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
						.addGroup(gl_userPanel.createSequentialGroup()
							.addGap(53)
							.addComponent(telTxt, GroupLayout.PREFERRED_SIZE, 34, GroupLayout.PREFERRED_SIZE)))
					.addGap(59)
					.addGroup(gl_userPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(email)
						.addComponent(emailTxt, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE))
					.addGap(72)
					.addGroup(gl_userPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(alterButton)
						.addComponent(cancelButton))
					.addContainerGap(83, Short.MAX_VALUE))
		);
		userPanel.setLayout(gl_userPanel);
		
		JLabel s_userName = new JLabel("用户名：");
		s_userName.setFont(new Font("宋体", Font.BOLD, 25));
		s_userName.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/bookAdmi/用户.png")));
		
		s_userNameTxt = new JTextField();
		s_userNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_userNameTxt.setColumns(10);
		
		JLabel s_passWord = new JLabel("密  码：");
		s_passWord.setFont(new Font("宋体", Font.BOLD, 25));
		s_passWord.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/bookAdmi/密码.png")));
		
		s_passWordTxt = new JTextField();
		s_passWordTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_passWordTxt.setColumns(10);
		
		JButton searchButton = new JButton("查 询");
		searchButton.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/bookAdmi/查询.png")));
		searchButton.setFont(new Font("宋体", Font.BOLD, 25));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				userImfSearchActionPerformed(e);
				
			}
		});
		
		JButton s_cancelButton = new JButton("取消");
		s_cancelButton.setFont(new Font("宋体", Font.BOLD, 25));
		s_cancelButton.setIcon(new ImageIcon(UserImfSearchInterFrm.class.getResource("/images/bookAdmi/重置.png")));
		s_cancelButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				searchResetActionPerformed(e);
			}
		});
		GroupLayout gl_userSearchPanel = new GroupLayout(userSearchPanel);
		gl_userSearchPanel.setHorizontalGroup(
			gl_userSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_userSearchPanel.createSequentialGroup()
					.addGap(33)
					.addComponent(s_userName)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_userNameTxt, GroupLayout.PREFERRED_SIZE, 171, GroupLayout.PREFERRED_SIZE)
					.addGap(82)
					.addComponent(s_passWord)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_passWordTxt, GroupLayout.PREFERRED_SIZE, 185, GroupLayout.PREFERRED_SIZE)
					.addGap(55)
					.addComponent(searchButton)
					.addGap(79)
					.addComponent(s_cancelButton)
					.addContainerGap(100, Short.MAX_VALUE))
		);
		gl_userSearchPanel.setVerticalGroup(
			gl_userSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_userSearchPanel.createSequentialGroup()
					.addGap(25)
					.addGroup(gl_userSearchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_userName)
						.addComponent(s_userNameTxt, GroupLayout.PREFERRED_SIZE, 39, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_passWord)
						.addComponent(s_passWordTxt, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
						.addComponent(searchButton)
						.addComponent(s_cancelButton))
					.addContainerGap(41, Short.MAX_VALUE))
		);
		userSearchPanel.setLayout(gl_userSearchPanel);
		getContentPane().setLayout(groupLayout);
		
		//设置JFrame居中显示
		this.setLocation(240, 20);
		
	}
	
	
	/**
	 * 取消修改事件处理
	 * @param e
	 */
	protected void updateResetActionPerformed(ActionEvent e) {
		this.reSetValue();
		
	}

	/**
	 * 个人信息查询
	 * @param e
	 */
	protected void userImfSearchActionPerformed(ActionEvent evt) {
		
//		String userName=logOnFrm.getUserName();
//		String passWord=logOnFrm.getPassWord();
		
		
		String userName=this.s_userNameTxt.getText();
		String passWord=this.s_passWordTxt.getText();
		
		
		User user=new User(userName,passWord);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=userDao.list(con, user);
			while(rs.next()) {
				this.idTxt.setText((String)rs.getString("id"));
				this.userNameTxt.setText((String)rs.getString("userName"));
				this.passWordTxt.setText((String)rs.getString("passWord"));
				this.telTxt.setText((String)rs.getString("tel"));
				this.emailTxt.setText((String)rs.getString("email"));
				String sex=rs.getString("sex");
				if("男".equals(sex)) {
					this.manJrb.setSelected(true);
				}else if("女".equals(sex)) {
					this.femaleJrb.setSelected(true);
				}

				String userType=rs.getString("userType");
				if("学生".equals(userType)) {
					this.userTypeJcb.setSelectedItem("学生");
				}
				else if("老师".equals(userType)) {
					this.userTypeJcb.setSelectedItem("老师");
				}
				else if("系统管理员".equals(userType)) {
					this.userTypeJcb.setSelectedItem("系统管理员");
				}
				else if("图书管理员".equals(userType)) {
					this.userTypeJcb.setSelectedItem("图书管理员");
				}
				
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 用户信息修改事件
	 * @param e
	 */
	protected void userUpdateActionPerformed(ActionEvent evt) {
		String id=this.idTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录！");
			return;
		}
			
			String userName=this.userNameTxt.getText();
			String passWord=this.passWordTxt.getText();
			String tel=this.telTxt.getText();
			String email=this.emailTxt.getText();
			String userType=(String)this.userTypeJcb.getSelectedItem();
			

			if(StringUtil.isEmpty(userName)) {
				JOptionPane.showMessageDialog(null, "用户名不能为空!");
				return;
			}
			
			if(StringUtil.isEmpty(passWord)) {
				JOptionPane.showMessageDialog(null, "密码不能为空!");
				return;
			}
			
			if(StringUtil.isEmpty(tel)) {
				JOptionPane.showMessageDialog(null, "手机号码不能为空!");
				return;
			}
			
			if(StringUtil.isEmpty(email)) {
				JOptionPane.showMessageDialog(null, "邮箱不能为空!");
				return;
			}
			
			
			String sex="";
			if(manJrb.isSelected()) {
				sex="男";
			}
			else if(femaleJrb.isSelected()) {
				sex="女";
			}
			
			User user=new User(Integer.parseInt(id),userName,passWord,sex,userType,tel,email);
			Connection con=null;
			try {
				con=dbUtil.getCon();
				int addNum=userDao.update(con, user);
				if(addNum==1) {
					JOptionPane.showMessageDialog(null,"用户信息修改成功" );
					reSetValue();
					
				}
				else {
					JOptionPane.showMessageDialog(null, "用户信息修改失败！");
				}
			}catch(Exception e) {
				e.printStackTrace();
				JOptionPane.showMessageDialog(null, "用户信息修改失败！");
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		/**
		 * 用户信息查询取消事件处理
		 * @param evt
		 */
	protected void searchResetActionPerformed(ActionEvent evt) {
		this.s_userNameTxt.setText("");
		this.s_passWordTxt.setText("");
	}
	
	
	/**
	 * 重置个人信息表单
	 */
	private void reSetValue() {
		this.idTxt.setText("");
		this.userNameTxt.setText("");
		this.passWordTxt.setText("");		
		this.manJrb.setSelected(true);		
		if(this.userTypeJcb.getItemCount()>0) {
			this.userTypeJcb.setSelectedIndex(0);		
		}		
		this.telTxt.setText("");
		this.emailTxt.setText("");
	}
}
